

showTree=function(context){
	window.colorfn=function () {};
	window.treeHeight=null;
	window.treeHeight2=null;
	window.treeHeight=new Array();
	window.context=context;
	window.treeNum=0;
	window.treeLev=0;
	window.treeCenterHeight=null;
	context.css("height",400);
	resultAjax(context.attr("url"),"",function(data,obj){
		
		addProperties(data);
		var num=sumItem(data,0);
		if(num>4)
		{
		
			window.treeNum=num;
			window.treeLev=maxLev;
			context.css("height",(num-3)*(layerPadding*2)+context.height());
			context.css("width",maxLev*(layerPadding+60)>context.width()?maxLev*(layerPadding+60):context.width());
			if(context.width() >context.parent().width())
			{
				context.parent().css("overflow-x","auto");
			}
		}
		window.treeData=data;
		init(context[0],data);
	},"json",context);
}

function parseName(name)
{
//	console.log("name:===>"+name);
	var result="";
	var len=0;
	var count=0;
	var i=0;
	var cellLen=10;
	
	while(count <=cellLen*2+1 && name.length >cellLen){
		count++;
		strCode = name.charCodeAt(i);//name.charAt(i);//
		i++;
		if (strCode > 130)//.match(/[\u0000-\u00ff]/g)!="null"
		{
			
			len++;
		}else{
			len+=0.5;
			console.log("===>"+strCode+"---->"+name.charAt(i));
		}
		if(len>=cellLen)
		{
			
			result+=name.substring(0,i)+"\n";
			name=name.substring(i);
			len=0;
			i=0;
			count=0;
		}
		
	}
	
	
	
	if(name== '')
	{
		result=result.substring(0,result.length-1);
	}
	
	return result+name;
}

//统计数据
sumItem=function(data,lev)
{
	lev++;
	maxLev=Math.max(maxLev,lev);
	if(data == undefined )
	{
		return 0;
	}
	var num=0;
	var znum=0;
	for(var item in data)
	{
		num++;
		znum+=sumItem(data[item].children,lev);
	}

	return znum>num?znum:(num==0?1:num);
};

var layerPadding=100;
var maxLev=1;
//添加属性
addProperties=function(obj)
{
	
	for(var item in obj)
	{
		
		var color="#1e81de";
		if(obj[item].TYPE==1)
		{
			color="#23a1b0";
		}else if(obj[item].TYPE==2)
		{
			color="#2aa779";
		}
		obj[item].name=parseName(obj[item].NAME);
		obj[item].symbol="rect";
		obj[item].itemStyle={
                normal: {
                    label: {
                        show: true
                    },
                    color:color
                },
                emphasis:{
                    label: {
                        show: true
                    },
                    color:color,
                    borderWidth: 0
                }
            };
		//var width=obj[item].NAME.length-12>0?(obj[item].NAME.length-12)*12+200:100;
		//layerPadding=120;//Math.max(width-70,layerPadding);
		var row=obj[item].name.split("\n").length;
		layerPadding=Math.max(row>0?154:0,100);//
		obj[item].symbolSize= [layerPadding, row>2?row*18:40];
		if(obj[item].children != undefined)
		{
			addProperties(obj[item].children);
		}
		obj[item].value=20;
		
	}
}


init=function(box,data){
	if(data == undefined || box ==undefined )
	{
		return;
	}
	console.log(data);
	require.box=box;
	//初始化控件
	require.config({
	    paths: {
	    	echarts: ui.url+'tags/reportforms/build/dist'
	    }
	});
	
	//初始化容器
	require(
	        [
	            'echarts',
	            'echarts/chart/tree' // 使用饼图就加载pie模块，按需加载
	        ],
	        function (ec) {
	        	window.myChart = ec.init(require.box);
	        	  window.colorfn=function () {
	      			var zrColor = require('zrender/tool/color');
	      			var obj=zrColor.getLinearGradient(
	      				0, 0, 90, 70,
	      				[[0, '#00008B'], [1, '#00BFFF']]
	      			);
	      			return obj;
	      		};
	      		//console.log(getRootLocationHeight()+"======>");
	        	myChart.setOption(getOption()); 
	        	
	        	window.context.css("height",getMax(window.treeHeight,0)-getMin(window.treeHeight,0)+layerPadding);
	        	window.context.find("div").css("height",getMax(window.treeHeight,0)-getMin(window.treeHeight,0)+layerPadding);
	        	
	        	
	        	console.log(window.treeCenterHeight+"min:"+getMin(window.treeHeight,0)+"    max:"+getMax(window.treeHeight,0));
	        	window.treeHeight2=1;
	        	myChart.setOption(getOption()); 
	        	
	        	console.log(window.treeCenterHeight+"min:"+getMin(window.treeHeight2,0)+"    max:"+getMax(window.treeHeight2,0));
	        	
	        });
}

function getMax(a,i)
{
	
	if(a.length >i+1)
	{
		return Math.max(window.treeHeight[i],getMax(window.treeHeight,++i))
	}
	return window.treeHeight[i];
}

function getMin(a,i)
{
	if(a.length >i+1)
	{
		return Math.min(window.treeHeight[i],getMin(window.treeHeight,++i))
	}
	return window.treeHeight[i];
}

function getRootLocationHeight()
{
	if(window.treeCenterHeight == undefined){
		return '50%';
	}
	var t=window.treeCenterHeight- getMin(window.treeHeight,0)+40;
	var a=window.treeCenterHeight*2;
	return ""+(t/a)*100+"%";
}

function getOption(){
	
	option = {
		    title : {
		        text: '',
		        subtext: ''
		    },
		   
		    calculable : false,
		    clickable: false,		
		    series : [
		        {
		            name:'树图',
		            type:'tree',
		            orient: 'horizontal',  // vertical horizontal
		            rootLocation: {x: 100, y: getRootLocationHeight()}, // 根节点位置  {x: 'center',y: 10}
		            
		            symbol: 'circle',
		            //hoverable: false,
		            //roam: true,
		            //symbolSize: 10,
		            nodePadding:30,
		            layerPadding:layerPadding+10,
		            
		            itemStyle: {
		                normal: {
		                    label: {
		                        show: true,
		                        position: 'inside',
		                        textStyle: {
		                            color: '#ffffff',
		                            fontSize: 14
		                          
		                        }
		                    },
		                    lineStyle: {
		                        color: "#1e81de",
		                        width: 1,
		                        type: 'broken' // 'curve'|'broken'|'solid'|'dotted'|'dashed'
		                    }
		                },
		                emphasis: {
		                    label: {
		                        show: false
		                    }
		                   
		                }
		            },
		            data: [
		                {
		                    name: '组织架构图',
		                    value: 6,
		                    symbolSize: [100, 70],
							symbol:'rect',
		                    //symbol: 'image://http://www.iconpng.com/png/ecommerce-business/iphone.png',
		                    itemStyle: {
		                        normal: {
		                            label: {
		                                show: true
		                            },
									 color:"#1e81de"/*(function () {
											var zrColor = require('zrender/tool/color');
											var obj=zrColor.getLinearGradient(
												0, 0, 90, 70,
												[[0, '#00008B'], [1, '#00BFFF']]
											);
											return obj;
										})()*/
									 
		                        },
		                        emphasis:{
		                        	 label: {
			                                show: true
			                            },
										 color:"#1e81de",
										 borderWidth: 0
		                        }
		                
		                    },
		                    children: window.treeData
		                }
		            ]
		        }
		    ]
		};
		return option;
	}


resultAjax=function(url,data,m_fun,type,result){
	console.log(">>>>>"+url);
	var conf={"stringFormat":true};
	$().invoke(url,data,[function(m,r){
		if (type=='JSON'||type=='json'){
			r=jQuery.parseJSON(r);
		}
		m_fun.apply(self, [r,result] );
	}],conf);
	return;
}